package com.ybkj.daijia.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.common.MediaUtil;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.DriverApplyMapper;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverApply;
import com.ybkj.daijia.server.service.DriverApplyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.serverUtils.IOUtils;
import com.ybkj.daijia.serverUtils.WebUtils;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Shine
 */
@Slf4j
@Service
public class DefaultDriverApplyService implements DriverApplyService {

    @Autowired
    private DriverApplyMapper driverApplyMapper;
    @Autowired
    private DriverService driverService;

    @Override
    public void driverAppApply(HttpServletRequest req, String realName, String phone, MultipartFile photoFile, String emergency, String bankNo, String jobType, MultipartFile idCardPhoto,
        MultipartFile backOfIdCardPhoto, String bankCard, String idCard, MultipartFile licenseFile, MultipartFile travelFile, String genger, String nation, String birthday, String origin,
        String driverCensus, String driverIdcardValid, String driverIdcardInvalid, String bankName, String driverLicenseNo, String address, String vehicleType, String validStartDate,
        String validEndDate, String archiveNum, String introducer, Boolean copy, Boolean remake) {
    }

    /***
     * 检验存储照片文件
     * @param qualificationPhoto
     * @param formateStr
     * @param path
     * @return
     * @throws IOException
     */
    public String checkAndSavePhoto(MultipartFile qualificationPhoto, String formateStr, String path) {
        if (null != qualificationPhoto) {
            boolean isPicture = false;
            try {
                if (MediaUtil.isPicture(qualificationPhoto, "")) {
                    isPicture = true;
                }
            } catch (IOException e) {
                if (log.isErrorEnabled()) {
                    log.error("文件检查错误", e);
                }
                throw ErrorConstantV4.IMG_NAME_ERROR.changeToCanThrowToUserException();
            }
            if (!isPicture) {
                throw ErrorConstantV4.IMG_NAME_ERROR.changeToCanThrowToUserException();
            }
            return IOUtils.saveFileInLocalByMultipartFileFormateNameAndPath(path, qualificationPhoto, formateStr);
        }
        return null;
    }

    /* (non-Javadoc)
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageInfo<DriverApply> queryAll(Integer pageNumber, Integer pageSize) {
        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());
        PageHelper.startPage(Pager.getPage(), Pager.getLimit());
//        long total = driverApplyMapper.countByPage();
//        return new PageInfo<DriverApply>(content, pageable, total);
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DriverApply insertRecord(DriverApply driverApply) {
        driverApplyMapper.insertRecord(driverApply);
        return driverApply;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void driverAppApply(HttpServletRequest req, MultipartFile photoFile, MultipartFile idCardPhotoFile, MultipartFile backOfIdCardPhotoFile, MultipartFile licenseFile,
        MultipartFile qualificationPhotoFile,
        DriverApply driverApply, Boolean copy, Boolean remake) {
        String introducer = driverApply.getIntroducer();
        if (StringUtils.isNotBlank(introducer)) {
            Driver introducerDriver = driverService.findByUsername(introducer);
            if (null != introducerDriver) {
                driverApply.setIntroducer(introducer);
            }
        }
        String formateStr = "%s.png";
        String bathPath = WebUtils.getBasePath(req);
        driverApply.setQualificationPhoto(checkAndSavePhoto(qualificationPhotoFile, formateStr, bathPath + Driver.QUALIFICATION_PHOTO_PATH));
        if (null != photoFile) {
            boolean isPicture = false;
            try {
                if (MediaUtil.isPicture(qualificationPhotoFile, "")) {
                    isPicture = true;
                }
            } catch (IOException e) {
                if (log.isErrorEnabled()) {
                    log.error("文件检查错误", e);
                }
                throw ErrorConstantV4.IMG_NAME_ERROR.changeToCanThrowToUserException();
            }
            if (!isPicture) {
                throw ErrorConstantV4.IMG_NAME_ERROR.changeToCanThrowToUserException();
            }
            String path = Driver.DRIVER_PATH;
            driverApply.setPhoto(IOUtils.savePhotoFileInLocalByMultipartFileFormateNameAndPath(bathPath + path, photoFile, formateStr, new int[]{400, 400}));
        }
        driverApply.setIdCardPhoto(checkAndSavePhoto(idCardPhotoFile, formateStr, bathPath + Driver.ID_CARD_PHOTO_PATH));
        driverApply.setBackOfIdCardPhoto(checkAndSavePhoto(backOfIdCardPhotoFile, formateStr, bathPath + Driver.ID_CARD_PHOTO_BACK_PATH));
        driverApply.setLicensePhoto(checkAndSavePhoto(licenseFile, formateStr, bathPath + Driver.LICENSE_PATH));
        insertRecord(driverApply);
    }

    @Override
    public DriverApply findOne(Long id) {
        return driverApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(DriverApply driverApply) {
        driverApplyMapper.deleteByPrimaryKey(driverApply.getId());
    }

    @Override
    public DriverApply findByIdCard(String idCard) {
//        return driverApplyMapper.selectByIdCard(StringUtils.trim(idCard));
        return null;
    }

    @Override
    public DriverApply findByPhone(String phone) {
        return null;
//        return driverApplyMapper.selectByPhone(StringUtils.trim(phone));
    }

    @Override
    public long countAll() {
        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();
        if (!principal.isCompanyType()) {
            return 0;
        }
//        return driverApplyMapper.countByPage();
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(DriverApply driverApply) {
        return driverApplyMapper.updateByPrimaryKey(driverApply);
    }
}